bool isDelimiters (char ch)
{
	static string deliemeters(" ;.?");
	if ( std::string::npos == deliemeters.find(ch) ) 
		return false;
	return true;
}

bool  reverseString(string & inputString,const  int  & wordStartigPosition,const  int & wordEndingPosition)
{
   // Step 1 : Check of invalid cases first and make your function edge cases-proof
    if(wordStartigPosition > wordEndingPosition) // invalid input
   	 return false; 
    int length = inputString.length(); 

    if( length-1< wordStartigPosition  || wordStartigPosition < 0) // invalid input
   	 return false; 
    if( length-1<wordEndingPosition || wordEndingPosition <0) //invalid input
   	 return false;

  // Step 2: Now real reversal begins
    int i = wordStartigPosition, j = wordEndingPosition;    
    while ( i<= j)
    {
   	 char temp = inputString.at(i);
   	 inputString.at(i) = inputString.at(j);
   	 inputString.at(j) = temp;
   	 ++i;--j;
    }
    return true;
}
void reverseContentsOfWordsInString(string & inputString)
{
    int len = inputString.length();    
    if(len < 2)
   	 return;
    bool isInWord = false;
    int wordCount = 0;
    int wordStartigPosition = 0,wordEndingPosition = 0;
    char ch =0;
    for(int i = 0 ; i <= len; ++i)
    {
   	 if (i<len)
   	 {
   		 ch =  inputString.at(i);
   	 }
   	 else // no trailing space...
   	 {
   		 ch = ' ';  //this is an important case 		 
   	 }
   	 bool isDelimitersChar  = isDelimiters (ch);
   	 if( false == isDelimitersChar  && false == isInWord )
   	 { // means word has just began and this is the first character.
   		 isInWord = true;
   		 wordStartigPosition = i;
   	 }
   	 else if (true == isDelimitersChar  && true == isInWord)
   	 { //word has ended
   		 isInWord = false;
   		 wordEndingPosition = i-1;
   		 bool bResult = reverseString(inputString,wordStartigPosition,wordEndingPosition);
		if( false == bResult ) 
			return ;
   		 wordStartigPosition = 0;
   		 wordEndingPosition = 0;
   	 }
   	 else if ( false == isDelimitersChar  && true == isInWord )
   	 {
   		 // Delimiters  is not seen , we are still in word ... keep going
   	 }
   	 else if ( true  == isDelimitersChar  && false == isInWord )
   	 {
   		 // we are not in word so it really doesn't matter...
   		 // this could be multiple spaces( or other Delimiters s ) between two words
   	 }    
    }
}

int main()
{
	string str("Reverse the contents of words");
	reverseContentsOfWordsInString(str);
	cout<<"reversed contents :"<<str<<endl;
        return 0;
}